package com.redsoil.house.ui.capital.fragment;

import android.os.Bundle;

import com.redsoil.house.R;
import com.redsoil.house.base.RootFragment;
import com.redsoil.house.ui.capital.contract.CapitalContract;
import com.redsoil.house.ui.capital.presenter.CapitalPresenter;
import com.redsoil.house.widget.TopMenuBarItem;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * 资产主界面
 */
public class CapitalFragment extends RootFragment<CapitalPresenter> implements CapitalContract.View {

    @BindView(R.id.top_bar_capital)
    TopMenuBarItem top_bar_capital;

    @BindView(R.id.top_bar_property)
    TopMenuBarItem top_bar_property;

    public static final int FIRST = 0;
    public static final int SECOND = 1;
    private SupportFragment[] mFragments = new SupportFragment[2];

    public static CapitalFragment newInstance() {
        Bundle args = new Bundle();

        CapitalFragment fragment = new CapitalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initEventAndData() {

        SupportFragment firstFragment = findFragment(CapitalInfoFragment.class);

        if (firstFragment == null) {
            mFragments[FIRST] = CapitalInfoFragment.newInstance();
            mFragments[SECOND] = CapitalPropertyListFragment.newInstance();

            loadMultipleRootFragment(R.id.capital_container, FIRST,
                    mFragments[FIRST], mFragments[SECOND]);

        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            // 这里我们需要拿到mFragments的引用,也可以通过getChildFragmentManager.findFragmentByTag自行进行判断查找(效率更高些),用下面的方法查找更方便些
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findFragment(CapitalPropertyListFragment.class);
        }
        topBarCapitalClick();
    }

    @OnClick(R.id.top_bar_capital)
    void topBarCapitalClick() {
        top_bar_capital.setSelected(true);
        top_bar_property.setSelected(false);
        showHideFragment(mFragments[0], mFragments[1]);
    }

    @OnClick(R.id.top_bar_property)
    void topBarPropertyClick() {
        top_bar_capital.setSelected(false);
        top_bar_property.setSelected(true);
        showHideFragment(mFragments[1], mFragments[0]);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_capital;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
}
